﻿using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Eneta.Portal.Common.Helpers
{
    /// <summary>
    /// Class for URL operations.
    /// </summary>
    public class UrlHelper
    {
        /// <summary>
        /// Gets the name of the page file. This method replaces all forbidden and special 
        /// characters in raw file name.
        /// </summary>
        /// <param name="name">The raw file name.</param>
        public static string GetPageFileName(string name)
        {
            var replacements = new Dictionary<string, string>
                                   {
                                       {"õ", "o"},
                                       {"ä", "a"},
                                       {"ö", "o"},
                                       {"ü", "u"},
                                       {"Õ", "o"},
                                       {"Ä", "a"},
                                       {"Ö", "o"},
                                       {"Ü", "u"},
                                       {" ", "-"},
                                       {"#", ""},
                                       {"%", ""},
                                       {"&", ""},
                                       {"*", ""},
                                       {":", ""},
                                       {"<", ""},
                                       {">", ""},
                                       {"?", ""},
                                       {"/", ""},
                                       {"{", ""},
                                       {"|", ""},
                                       {"}", ""},
                                       {"'", ""},
                                       {"’", ""},
                                       {"´", ""},
                                       {"`", ""},
                                       {"!", ""},
                                       {"=", ""},
                                       {"\"", ""},
                                       {".", " "},
                                       {",", " "},
                                       {"_", " "}
                                   };

            var pageName = name;
            foreach (var s in replacements.Keys)
                pageName = pageName.Replace(s, replacements[s]);

            pageName = pageName.Replace("-", " ");
            pageName = Regex.Replace(pageName, @"\s+", " ").Trim();
            pageName = Regex.Replace(pageName, @"\W+", " ").Trim();
            pageName = pageName.Replace(" ", "-");
            pageName += ".aspx";
            pageName = pageName.ToLower();
            return pageName;
        }

        /// <summary>
        /// Gets the profile URL based on given profile id.
        /// </summary>
        /// <param name="id">The profile id.</param>
        public static string GetProfileUrl(int id)
        {
            return GetProfileUrl(id, string.Empty);
        }

        /// <summary>
        /// Gets the profile URL based on given profile id and message id.
        /// </summary>
        /// <param name="id">The profile id.</param>
        /// <param name="msg">The message id.</param>
        public static string GetProfileUrl(int id, string msg)
        {
            return GetProfileUrl(id.ToString(), msg);
        }

        /// <summary>
        /// Gets the profile URL based on given profile id and message id.
        /// </summary>
        /// <param name="id">The profile id.</param>
        /// <param name="msg">The message id.</param>
        public static string GetProfileUrl(string id, string msg)
        {
            var url = "/profile.aspx?ID=" + id;
            if (!string.IsNullOrEmpty(msg))
            {
                url += ("&msg=" + msg);
            }
            return url;
        }

        /// <summary>
        /// Gets the user image handler URL.
        /// </summary>
        /// <param name="size">The size of user avatar (16, 32, 64).</param>
        /// <param name="profileId">The profile id.</param>
        public static string GetUserImageHandlerUrl(int size, int profileId)
        {
            return string.Format("/_layouts/enetahandlers/userimagehandler.ashx?size={0}&id={1}", size, profileId);
        }
    }
}
